package net.dgg.zz.credential.talent.controller;

import com.alibaba.fastjson.JSONObject;
import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.treebook.entity.TreeBook;
import net.dgg.tmd.foundation.platform.user.service.UserService;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.service.CommonMangerExtService;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.common.utils.excel.ExcelUtils;
import net.dgg.zz.credential.company.service.CompanyService;
import net.dgg.zz.credential.talent.entity.Talent;
import net.dgg.zz.credential.talent.entity.TalentRecord;
import net.dgg.zz.credential.talent.service.TalentRecordService;
import net.dgg.zz.credential.talent.service.TalentService;
import net.dgg.zz.credential.talent.vo.TalentVo;
import net.dgg.zz.production.vo.InprocessOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/zs/talent")
public class TalentController extends DggBaseController {
    @Autowired
    private CommonService commonService;
    @Autowired
    private TalentService talentService;
    @Autowired
    private DesUtil desUtil;
    @Autowired
    private CommonMangerExtService zzCommonMangerExtService;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private TalentRecordService talentRecordService;

    /**
     * 人才库页面
     * @param model
     * @return
     */
    @RequestMapping(value="/index")
    public String index(Model model){
        model.addAttribute("talentStatus", commonService.queryTreeBook("TALENT_STATUS",1,2,null));
        model.addAttribute("talentEdcation",commonService.queryTreeBook("TALENT_EDUCATION",1,2,null));
        model.addAttribute("jobStatus",commonService.queryTreeBook("TALENT_JOB_STATUS",1,2,null));
        model.addAttribute("talentPosition",commonService.queryTreeBook("TALENT_POSITION",1,2,null));
        model.addAttribute("sex",commonService.queryTreeBook("CMS_SEX",1,2,null));
        model.addAttribute("user",sessionManager.getCurrentUser());
        return "zzsc/credential/talent/index";
    }

    /**
     * 人才列表
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value="/talent_list")
    @ResponseBody
    public DataTableResponse queryTalentList(@RequestParam Map params, HttpServletRequest request){
        try {
            DataTableRequest r = DataTableUtils.getParam(request); //获取datatable参数
            params.put("start", r.getStart());//获取当前页
            params.put("limit", r.getLength());//获取每页条数
            List<Talent> list = talentService.selectTalentByWithPage(params);
            Integer count = talentService.count(params);
            return new DataTableResponse(r.getDraw(), count, count, list, "");
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 新增页面
     * @param model
     * @return
     */
    @RequestMapping(value="/creatTalent")
    public String createTalent(Model model){
        model.addAttribute("id", DggKeyWorker.nextId());
        model.addAttribute("sex",commonService.queryTreeBook("CMS_SEX",1,2,null));
        model.addAttribute("talentStatus", commonService.queryTreeBook("TALENT_STATUS",1,2,null));
        model.addAttribute("talentEdcation",commonService.queryTreeBook("TALENT_EDUCATION",1,2,null));
        model.addAttribute("talentPosition",commonService.queryTreeBook("TALENT_POSITION",1,2,null));
        model.addAttribute("jobStatus",commonService.queryTreeBook("TALENT_JOB_STATUS",1,2,null));
        return "zzsc/credential/talent/create";
    }

    /**
     * 新增页面
     * @return
     */
    @RequestMapping(value="/add_production_configuration")
    public String addProductionConfiguration(Model model){
        model.addAttribute("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        return "zzsc/all/add_production_configuration";
    }




    /**
     * 编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value="/editTalent")
    public String editTalent(Model model,Long id){
        model.addAttribute("sex",commonService.queryTreeBook("CMS_SEX",1,2,null));
        model.addAttribute("talentStatus", commonService.queryTreeBook("TALENT_STATUS",1,2,null));
        model.addAttribute("talentEdcation",commonService.queryTreeBook("TALENT_EDUCATION",1,2,null));
        model.addAttribute("talentPosition",commonService.queryTreeBook("TALENT_POSITION",1,2,null));
        model.addAttribute("jobStatus",commonService.queryTreeBook("TALENT_JOB_STATUS",1,2,null));
        model.addAttribute("talent",talentService.getTalenById(id));
        return "zzsc/credential/talent/edit";
    }

    /**
     * 添加数据
     * @param params
     * @return
     */
    @RequestMapping(value="/addTalent")
    @ResponseBody
    public DggRestResponse addTalent(@RequestParam Map params){
        try {
            boolean isExist = talentService.isExist(params.get("talentName").toString(),params.get("talentPhone").toString());
            if(isExist){
                return this.getFailResponse("人才库已存在该人才！");
            }
            talentService.insert(params);
            return this.getSuccessResponse("保存成功");
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

    /**
     * 保存数据
     * @param params
     * @return
     */
    @RequestMapping(value="/saveTalent")
    @ResponseBody
    public DggRestResponse saveTalent(@RequestParam Map params){
        try {
             Talent talent = talentService.getTalenById(Long.valueOf(params.get("id").toString()));
             if(!talent.getTalentPhone().equals(params.get("talentPhone").toString())){
                 boolean isExist = talentService.isExistEdit(params);
                 if(isExist){
                     return this.getFailResponse("人才库已存在该人才！");
                 }
             }
            talentService.updateTalent(params);
            return this.getSuccessResponse("保存成功");
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

    /**
     * 查看号码
     * @param id
     * @return
     */
    @RequestMapping(value="/see_number")
    @ResponseBody
    public DggRestResponse seeNumber(@RequestParam Long id){
        try {
            String returnStr = talentService.getTalentPhoneById(id);
            return this.getSuccessResponse(returnStr);

        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

    /**
     * 打电话功能查找人才
     * @param id
     * @return
     */
    @RequestMapping(value="/find_talent")
    @ResponseBody
    public DggRestResponse findTalentById(@RequestParam Long id){
        try {
           Talent talent = talentService.getTalenByIdNotDe(id);
            Map<String, Object> talentInfo = new HashMap<>();
            talentInfo.put("id", talent.getId());
            talentInfo.put("name", talent.getTalentName());
            Map noMap = new HashMap();
            noMap.put("phoneStr",desUtil.decrypt(talent.getTalentPhone()));
            noMap = CustomerNumberEncryptionUtil.encryptionObject(new String[]{"phoneStr"},noMap);
            talentInfo.put("phone", talent.getTalentPhone());
            talentInfo.put("phoneStr", noMap.get("phoneStr"));
            return this.getSuccessResponse(talentInfo);
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }
    @RequestMapping(value="/downLoadFile")
    public String downLoadFile(Model model,Long id){
        model.addAttribute("id", id);
        return "zzsc/credential/talent/file";
    }

    /**
     * 更新状态页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value="/statePage")
    public String statePage(Model model,Long id){
        Talent talent = talentService.getTalenById(id);
        model.addAttribute("state",talent.getTalentStatus());
        model.addAttribute("id",id);
        model.addAttribute("talentStatus", commonService.queryTreeBook("TALENT_STATUS",1,2,null));
        model.addAttribute("com",companyService.getCompanyList());
        return "zzsc/credential/talent/state";
    }

    /**
     * 更新状态
     * @param params
     * @return
     */
    @RequestMapping(value="/update")
    @ResponseBody
    public DggRestResponse update(@RequestParam Map params){
        try {
            talentService.updateStatus(params);
            return this.getSuccessResponse("保存成功");
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }
    @RequestMapping("/export_talent")
    public void exportSczzAbnormal(@RequestParam Map params, HttpServletResponse response) {
        try {
            String fileName = "人才库_" + DateUtils.dateString(new Date(), "yyyyMMddHHmm") + ".xlsx";
            List<TalentVo> dataList = talentService.findExportTalent(params);
            ExcelUtils.writeToFile(dataList, null, fileName, response, TalentVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping(value="recordPage")
    public String recordPage(Long id,Model model){
        model.addAttribute("id",id);
        return "zzsc/credential/talent/talent_record";
    }

    /**
     * 获取人才库备注列表
     * @param talentId
     * @param request
     * @return
     */
    @RequestMapping(value="/record_list")
    @ResponseBody
    public DataTableResponse queryRecordList(@RequestParam Long talentId, HttpServletRequest request){
        try {
            Map params = new HashMap();
            params.put("talentId",talentId);
            DataTableRequest r = DataTableUtils.getParam(request); //获取datatable参数
            params.put("start", r.getStart());//获取当前页
            params.put("limit", r.getLength());//获取每页条数
            List<TalentRecord> list = talentService.getRecordList(params);
            Integer count =talentRecordService.count(talentId);
            return new DataTableResponse(r.getDraw(), count, count, list, "");
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存数据
     * @param params
     * @return
     */
    @RequestMapping(value="/saveRecord")
    @ResponseBody
    public DggRestResponse saveRecord(@RequestParam Map params){
        try {
            talentService.saveRecord(params);
            return this.getSuccessResponse("保存成功");
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }
}
